package com.mspbots.rule.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @ClassName com.mspbots.rule.config.RabbitConfiguration
 * @Description TODO
 * @Author Jun
 * @Date 2019/8/14 10:37
 * @Version 1.0
 **/
@Configuration
public class RabbitConfiguration {


    private static Map<String, String> ATTENDANCE_QUEUES = new HashMap<>() {{
        put("trigger.attendance.BeforeWorkStart", "");
        put("trigger.attendance.BeforeWorkEnd", "");
        put("trigger.attendance.LateWorkStart", "");
        put("trigger.attendance.LateWorkEnd", "");
        put("trigger.attendance.LunchTooLong", "");
        put("trigger.attendance.BreakTooLong", "");
        put("trigger.attendance.NotEodTooLong", "");
    }};
    private static Map<String, String> TSHEET_QUEUES = new HashMap<>() {{
        put("trigger.tsheet.TsheetInCheck", "");
    }};

    private static Map<String, String> QUEUES = new HashMap<>() {{
        put("trigger.escalation.EscalationUser", "trigger.escalationUser.#");
        put("trigger.escalation.EscalationManager", "trigger.escalationManager.#");
    }};
    @Autowired
    private AmqpAdmin amqpAdmin;

    @Bean
    public void declareRuleAi() {
//        this.amqpAdmin.deleteExchange("msp.rule.ai");
//        this.amqpAdmin.deleteExchange("msp.wise.entries");
//        this.amqpAdmin.deleteExchange("msp.trigger.attendance");
//        this.amqpAdmin.deleteExchange("msp.wise.sheet.times");


        Exchange ruleAi = ExchangeBuilder.directExchange("msp.rule.ai").durable(false).autoDelete().build();
        this.amqpAdmin.declareExchange(ruleAi);

        Queue queueMessageSend = new Queue("message.send", false, false, true);
        this.amqpAdmin.declareQueue(queueMessageSend);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueMessageSend)
                .to(ruleAi).with("queue.send").noargs());

        Queue queueMessageSendForward = new Queue("message.sendForward", false, false, true);
        this.amqpAdmin.declareQueue(queueMessageSendForward);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueMessageSendForward)
                .to(ruleAi).with("queue.sendForward").noargs());


        Queue queueMessageSendTemp = new Queue("schedule.send", false, false, true);
        this.amqpAdmin.declareQueue(queueMessageSendTemp);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueMessageSendTemp)
                .to(ruleAi).with("schedule.send").noargs());

        Queue queueNotEnogh = new Queue("trigger.NotEnoughTimeLogged", false, false, true);
        this.amqpAdmin.declareQueue(queueNotEnogh);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueNotEnogh)
                .to(ruleAi).with("trigger.NotEnoughTimeLogged").noargs());

    }


    @Bean
    public void declareAttendance() {
        Exchange attendanceExchange = ExchangeBuilder.fanoutExchange("msp.trigger.attendance").build();
        this.amqpAdmin.declareExchange(attendanceExchange);

        Iterator<String> iterator = ATTENDANCE_QUEUES.keySet().iterator();
        while (iterator.hasNext()) {
            String queueName = iterator.next();

            Queue queue = QueueBuilder.durable(queueName).build();
            this.amqpAdmin.declareQueue(queue);
            this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                    .to(attendanceExchange).with(ATTENDANCE_QUEUES.get(queueName)).noargs());
        }
    }


/*    @Bean
    public void declareTsheet() {
        Exchange timeEntryExchange = ExchangeBuilder.fanoutExchange("msp.wise.sheet.times").build();
        this.amqpAdmin.declareExchange(timeEntryExchange);

        for (String queueName : TSHEET_QUEUES.keySet()) {
            Queue queue = QueueBuilder.durable(queueName).build();
            this.amqpAdmin.declareQueue(queue);
            this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                    .to(timeEntryExchange).with(TSHEET_QUEUES.get(queueName)).noargs());
        }
    }*/

    @Bean
    public void declareTopic() {
        Exchange timeEntryExchange = ExchangeBuilder.topicExchange("msp.rule.topic").build();
        this.amqpAdmin.declareExchange(timeEntryExchange);

        for (String queueName : QUEUES.keySet()) {
            Queue queue = QueueBuilder.nonDurable(queueName).autoDelete().build();
            this.amqpAdmin.declareQueue(queue);
            this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                    .to(timeEntryExchange).with(QUEUES.get(queueName)).noargs());
        }
    }


    @Bean
    public MessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }
}
